package internact;

import javax.swing.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会员管理类
 */
public class MemberManager {
    private Map<String, Member> members;

    public MemberManager() {
        members = new HashMap<>();
    }

    public boolean addMember(String username, String password, String name, String phone, double fee) {
        if (!members.containsKey(username)) {
            Member member = new Member(username, password, name, phone, fee);
            members.put(username, member);
            return true;
        } else {
            return false;
        }
    }

    public boolean deleteMember(String username) {
        if (members.containsKey(username)) {
            members.remove(username);
            return true;
        } else {
            return false;
        }
    }

    public boolean modifyMember(String username, String password, String name, String phone, double fee) {
        if (members.containsKey(username)) {
            Member member = members.get(username);
            member.setPassword(password);
            member.setName(name);
            member.setPhone(phone);
            member.setFee(fee);
            return true;
        } else {
            return false;
        }
    }

    public void displayAllMembers() {
        List<String> memberInfos = new ArrayList<>();
        for (Member member : members.values()) {
            memberInfos.add(member.toString());
        }
        JOptionPane.showMessageDialog(null, String.join("\n", memberInfos));
    }

    public void displayMemberInfo(String username) {
        if (members.containsKey(username)) {
            Member member = members.get(username);
            JOptionPane.showMessageDialog(null, member.toString());
        } else {
            JOptionPane.showMessageDialog(null, "会员不存在！");
        }
    }

    public void displayMemberLogoutRecords(String username) {
        if (members.containsKey(username)) {
            Member member = members.get(username);
            List<String> records = member.getLogoutRecords();
            if (records.isEmpty()) {
                JOptionPane.showMessageDialog(null, "该会员没有注销记录！");
            } else {
                JOptionPane.showMessageDialog(null, String.join("\n", records));
            }
        } else {
            JOptionPane.showMessageDialog(null, "会员不存在！");
        }
    }
}

